//
//  WBPopOverPresentationController.swift
//  weibo
//
//  Created by Felix on 16/11/6.
//  Copyright © 2016年 Felix. All rights reserved.
//

import UIKit

class WBPopOverPresentationController: UIPresentationController {

    ///定义一个变量
    var presentFrame = CGRect()
    
    ///重写系统构造方法
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
    }
    
    ///重写系统构造方法、用于设置自定义内容
    override func containerViewWillLayoutSubviews() {
        //containerView 容器
        //presentedView 目标
        //1.拿到需要modal的视图并且改变frame
        presentedView?.frame = presentFrame
        
        
        //2.在容器视图中加入覆盖层按钮
        containerView?.insertSubview(coverButton, at: 0)
        coverButton.addTarget(self, action: #selector(coverButtonClick), for: UIControlEvents.touchUpInside)
        
    }
    
    
    //拿到modal出来的控制器，并让其消失
    @objc private func coverButtonClick(){
        presentedViewController.dismiss(animated: true, completion: nil)
    }
    
    
    //懒加载一个覆盖层按钮
    private lazy var coverButton:UIButton = {
        let btn = UIButton.init()
        btn.frame = UIScreen.main.bounds
        btn.backgroundColor = UIColor.clear
        return btn
    }()
    
}
